﻿using System.Diagnostics;
using LFSRecord2.Misc;

namespace LFSRecord2.Model.UserActions
{
    public class UserActionKeyFrameDoubleValueChanged : UserActionBase
    {
        // Action description, so we can replay this action (and reverse it)
        public int LayerIndex { get; set; }
        public int PropertyIndex { get; set; }
        public int KeyFrameIndex { get; set; }
        public double ValueOffset { get; set; }

        public UserActionKeyFrameDoubleValueChanged()
        {
            Action = UserActionTypes.KeyFrameDoubleValueChanged;
        }

        public override void DoUndo()
        {
            Debug.WriteLine("Undoing a KeyFrameDoubleValueChanged action");
            if (KeyFrameIndex == -1)
            {
                ((CompLayerPropertyDouble)LfsRecordController.project.Layers[LayerIndex].Properties[PropertyIndex]).CurrentValue -= ValueOffset;
            }
            else
            {
                if (KeyFrameIndex > LfsRecordController.project.Layers[LayerIndex].Properties[PropertyIndex].KeyFrames.Count - 1)
                {
                    Logger.LogError("Invalid keyframe reference while undoing kfDoubleValueAction");
                }
                CustomDoubleKeyFrame kfd = (CustomDoubleKeyFrame)LfsRecordController.project.Layers[LayerIndex].Properties[PropertyIndex].KeyFrames[KeyFrameIndex];
                kfd.Value -= ValueOffset;
            }
            LfsRecordController.project.Layers[LayerIndex].UpdatedCurrentValues = CompLayerPropertyUpdateTypes.All;
                 
        }

        public override void DoRedo()
        {
            Debug.WriteLine("Redoing a KeyFrameDoubleValueChanged action");
            if (KeyFrameIndex == -1)
            {
                ((CompLayerPropertyDouble)LfsRecordController.project.Layers[LayerIndex].Properties[PropertyIndex]).CurrentValue += ValueOffset;
            }
            else
            {
                if (KeyFrameIndex > LfsRecordController.project.Layers[LayerIndex].Properties[PropertyIndex].KeyFrames.Count - 1)
                {
                    Logger.LogError("Invalid keyframe reference while redoing kfDoubleValueAction");
                }
                ((CustomDoubleKeyFrame)LfsRecordController.project.Layers[LayerIndex].Properties[PropertyIndex].KeyFrames[KeyFrameIndex]).Value += ValueOffset;
            }
            LfsRecordController.project.Layers[LayerIndex].UpdatedCurrentValues = CompLayerPropertyUpdateTypes.All;
             
        }
    }
}
